/*
 *  ICE_global_var.c
 *  ICE_Player
 *
 *  Created by xl on 11-7-3.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "ICE_global_var.h"

ICE_CONFIG* g_ice_cfg = 0;
WEB_SERVER_HANDLE g_web_server_handle = 0;
char* g_web_server_html_path = 0;
char g_cur_ip[MAX_IP_LEN] = {0};
char g_web_server_url[LOCAL_WEB_SERVER_LEN] = {0};
bool g_lock_screen_now = false;
bool g_is_in_player_now = false;
bool g_has_export_new_file = false;
char g_exported_new_file[MAX_ICE_URL_LEN] = {0};
FILE_INFO_LIST* g_current_file_list = NULL;
long g_cur_file_idx = 0;


////for configure utilities
static char* cfg_path = NULL;

ICE_CONFIG* gf_get_ice_cfg()
{
    if (!g_ice_cfg)
    {
        g_ice_cfg = icfg_create_configure();
        g_ice_cfg->is_files_locked = DEFAULT_CFG_FILE_LOCK;
        g_ice_cfg->is_rotation_locked = DEFAULT_CFG_ROTATION;
        g_ice_cfg->enable_subtitle = DEFAULT_CFG_SUBTITLE;
        g_ice_cfg->enable_text_info = DEFAULT_CFG_TEXT_INFO;
        g_ice_cfg->loop_mode = DEFAULT_CFG_LOOP_MODE;
        memset(g_ice_cfg->pass_word, 0, sizeof(g_ice_cfg->pass_word));
        if (!cfg_path)
        {
            cfg_path = malloc(MAX_FILE_NAME_LEN);
            sprintf(cfg_path, "%s%s", icfg_get_app_home_path(), CONFIG_FILE_PATH);
        }
        if(!icfg_get_configure_from_file(g_ice_cfg, cfg_path))
        {
            g_ice_cfg->is_files_locked = DEFAULT_CFG_FILE_LOCK;
            g_ice_cfg->is_rotation_locked = DEFAULT_CFG_ROTATION;
            g_ice_cfg->enable_subtitle = DEFAULT_CFG_SUBTITLE;
            g_ice_cfg->enable_text_info = DEFAULT_CFG_TEXT_INFO;
            g_ice_cfg->loop_mode = DEFAULT_CFG_LOOP_MODE;
            
            gf_set_ice_cfg(g_ice_cfg);
        }
    }
    return g_ice_cfg;
}

bool gf_set_ice_cfg(ICE_CONFIG* cfg)
{
    if (!cfg)
    {
        return false;
    }
    if (!cfg_path)
    {
        cfg_path = malloc(MAX_FILE_NAME_LEN);
        sprintf(cfg_path, "%s%s", icfg_get_app_home_path(), CONFIG_FILE_PATH);
    }
    if (!icfg_put_configure_to_file(cfg, cfg_path))
    {
        return false;
    }
    return true;
}
